//
//  HSYRootViewController+TopButton.m
//  高仿暴走斗图
//
//  Created by key on 16/9/29.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYRootViewController+TopButton.h"
#import "UIButton+RAC.h"
#import "NSString+ExchangeType.h"

static NSString *kTopButtonKey = @"TopButtonKey";

@implementation HSYRootViewController (TopButton)

//- (UIButton *)topButton
//{
//    UIButton *button = [self getPropertyForKey:[kTopButtonKey exchangeToChar]];
//    if (!button) {
//        @weakify(self);
//        button = [UIButton rac_createButtonForBackgroundImage:CREATE_IMG(SHOWVC_DOT)
//                                                withHighImage:CREATE_IMG(SHOWVC_DOT)
//                                                        title:nil
//                                                    highTitle:nil
//                                             signalCompletion:^(id x) {
//                                                    @strongify(self);
//                                                    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
//                                            }];
//        button.alpha = 0.0;
//        button.layer.cornerRadius = 25.0;
//        button.frame = CGRectMake(IPHONE_WIDTH - 60, IPHONE_HEIGHT - 183, 50, 50);
//        [self.view insertSubview:button aboveSubview:self.tableView];
//        [self setProperty:self forKey:[kTopButtonKey exchangeToChar] nonatomic:OBJC_ASSOCIATION_RETAIN_NONATOMIC];
//    }
//    return button;
//}

- (UIButton *)topButton
{
    return [self getPropertyForKey:[kTopButtonKey exchangeToChar]];
}

- (void)setTopButton:(UIButton *)topButton
{
    [self setProperty:topButton forKey:[kTopButtonKey exchangeToChar] nonatomic:OBJC_ASSOCIATION_RETAIN];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (!self.topButton) {
        return;
    }
    if (scrollView.contentOffset.y > 550.0) {
        if (self.topButton.alpha == 0.0) {
            [UIView animateWithDuration:1.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
                self.topButton.alpha = 0.8;
            } completion:^(BOOL finished) {
                self.topButton.alpha = 0.9;
            }];
        }
    } else {
        if (self.topButton.alpha > 0.0) {
            [UIView animateWithDuration:1.5 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                self.topButton.alpha = 0.2;
            } completion:^(BOOL finished) {
                self.topButton.alpha = 0.0;
            }];
        }
    }
}

@end
